import json

import requests

from yolo_web import app, config


def do_request(message, stream):
    data = {
        "model": "Baichuan4",
        "messages": [{"role": "user", "content": message}],
        "stream": stream,
        "tools": [{"type": "web_search", "web_search": {"enable": True}}],
    }

    json_data = json.dumps(data)

    headers = {
        "Content-Type": "application/json",
        "Authorization": "Bearer " + config.BC_API_KEY,
    }

    app.logger.info("send data:%s", json_data)
    return requests.post(
        config.BC_URL, data=json_data, headers=headers, timeout=60, stream=stream
    )
